{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1597392484726",
   "display_name": "Python 3.8.3 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。\n",
    "\n",
    "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素不能使用两遍。\n",
    "\n",
    " \n",
    "\n",
    "示例:\n",
    "```\n",
    "给定 nums = [2, 7, 11, 15], target = 9\n",
    "\n",
    "因为 nums[0] + nums[1] = 2 + 7 = 9\n",
    "所以返回 [0, 1]\n",
    "```\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/two-sum\n",
    "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "class Solution:\n",
    "    def twoSum(self, nums: List[int], target: int) -> List[int]:\n",
    "        mem = {}\n",
    "        for index in range(len(nums)):\n",
    "            if nums[index] in mem:\n",
    "                return [mem[nums[index]], index]\n",
    "            mem[target-nums[index]] = index\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert Solution().twoSum([2, 7, 11, 15], 9) == [0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1]"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "Solution().twoSum([2, 7, 11, 15], 9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "return 第一个数字的索引的时候，写错了，写成了在nums中找，没仔细想清楚，mem中的值，类似于{7: 0}，值还是要在mem中找的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在看java的题解中发现在结果可以抛出一个错误`throw new IllegalArgumentException(\"No two sum solution\");`一个错误，来提示没有找到答案，在动态语言中，或许可以通过这种方式来过编译。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改了一下go的代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```go\n",
    "func twoSum(nums []int, target int) []int {\n",
    "    m := map[int]int{}\n",
    "    for i:=0; i< len(nums); i++{\n",
    "        tmp := target - nums[i]\n",
    "        if index, ok := m[tmp]; ok{\n",
    "            return []int{i, index}\n",
    "        }\n",
    "        m[nums[i]] = i\n",
    "    }\n",
    "    panic(\"can't find\")\n",
    "    \n",
    "}\n",
    "```"
   ]
  }
 ]
}